﻿namespace ElegantRoute.Routing {
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Text;

    internal static class StringElegantHelper {

        public static readonly char minus = '-';

        public static string Elegant(string s) {
            var builder = new StringBuilder();
            var index = 0;
            foreach (var c in s) {
                if (c >= 'A' && c <= 'Z') {
                    if (index > 0) builder.Append(minus);
                    builder.Append(char.ToLower(c));
                }
                else if (c == minus) {
                    builder.Append(minus);
                    builder.Append(minus);
                }
                else
                    builder.Append(c);
                index++;
            }
            return builder.ToString();
        }

        public static string DeElegant(string s) {
            var builder = new StringBuilder();
            var iterator = s.GetEnumerator();
            while (iterator.MoveNext()) {
                if (iterator.Current != minus) {
                    builder.Append(iterator.Current);
                    continue;
                }
                if (!iterator.MoveNext()) {
                    builder.Append(minus);
                    break;
                }
                if (iterator.Current == minus)
                    builder.Append(minus);
                else
                    builder.Append(iterator.Current);
            }
            return builder.ToString();
        }
    }
}